package com.eryos.android.cigarettecounter.tool;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.content.Context;

/**
 * Project :  CigaretteCounter
 * Author :   Olivier Combe 
 * URL :      http://eryos.fr/CigaretteCounter/
 *
 * Copyright (C) 2011 - Olivier Combe 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */

public class Tools {

	/**
	 * Return the last day of the month in param
	 * @param month number ( from 1 to 12 )
	 * @return
	 */
	public static int getLastDayOfMonth( int month ){

		Calendar c = Calendar.getInstance(); 
		c.set(Calendar.MONTH, month);		// 15/03/20XX
		c.add(Calendar.MONTH, 1);			// 15/04/20XX
		c.set(Calendar.DAY_OF_MONTH, 1); 	// 01/04/20XX
		c.add(Calendar.DAY_OF_MONTH, -1);	// 31/03/20XX

		return c.get(Calendar.DAY_OF_MONTH);
	}
	
	
    
    /**
     * Last day of the month with data ( date format : 23/01/2011 )
     * @param ctx
     * @param _date
     * @return
     */
	public static String getMonthLabel( Context ctx, int month){
		
        Calendar c = Calendar.getInstance(); 
        c.set(Calendar.MONTH, month);		// 15/03/20XX
        c.set(Calendar.DAY_OF_MONTH, 1); 	// 01/04/20XX
   
        SimpleDateFormat sdf = new SimpleDateFormat("MMMMM");
        String monthLabel = sdf.format(c.getTime());
      return monthLabel;

    }
    
	/**
	 * 
	 * @param ctx
	 * @param month
	 * @return
	 */
    public static String getMonthFirstLetter(Context ctx, int month){
        Calendar c = Calendar.getInstance(); 
        c.set(Calendar.MONTH, month);		// 15/03/20XX
        c.set(Calendar.DAY_OF_MONTH, 1); 	// 01/04/20XX
   
        SimpleDateFormat sdf = new SimpleDateFormat("MMM");
        String monthLabel = sdf.format(c.getTime());
        return monthLabel;
    }
    
    /**
     * 
     * @param ctx
     * @param day
     * @return
     */
    public static String getDayFirstLetter(Context ctx, int day){
    
        Calendar c = Calendar.getInstance(); 
         c.set(Calendar.DAY_OF_WEEK, day); 	// 01/04/20XX
    
         SimpleDateFormat sdf = new SimpleDateFormat("E"); // Day in Week
         String dayLabel = sdf.format(c.getTime());
         return dayLabel;

    }
	
  public static String getTimestamp(){

		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH);
		int day = c.get(Calendar.DAY_OF_MONTH);
		
		int hour = c.get(Calendar.HOUR);
		int minute = c.get(Calendar.MINUTE);
		int second = c.get(Calendar.SECOND);
		
		// Put month value in range 1-12 instead of 0-11
		month++;
		
		String sYear = year+"";
		String sMonth = month+"";
		String sDay = day+"";
		
		String sHour = hour+"";
		String sMinute = minute+"";
		String sSecond = second+"";
		
		// Add Leading 0
		if ( sMonth.length() == 1 )
			sMonth = "0"+sMonth;
		
		if ( sDay.length() == 1 )
			sDay = "0"+sDay;
		
		if ( sHour.length() == 1 )
			sHour = "0"+sHour;
		
		if ( sMinute.length() == 1 )
			sMinute = "0"+sMinute;
		
		if ( sSecond.length() == 1 )
			sSecond = "0"+sSecond;
		
		return sYear+""+sMonth+""+sDay+"_"+sHour+""+sMinute;//+""+sSecond;
	}
   
}
